import datetime

from App.plugins import models
import uuid


class BaseModel(models.Model):
    __abstract__ = True
    id = models.Column(models.Integer, primary_key=True, autoincrement=True)
    uuid = models.Column(models.String(32), default=uuid.uuid4().hex)
    # 创建时间
    create_at = models.Column(models.DateTime, default=datetime.datetime.now)
    # 更新时间
    update_at = models.Column(models.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)

    def save(self):
        try:
            models.session.add(self)
            models.session.commit()
            return True
        except Exception as e:
            print(e)
            return False

    def merge(self):
        try:
            models.session.merge(self)
            models.session.commit()
            return True
        except Exception as e:
            print(e)
            return False

    def delete(self):
        try:
            models.session.delete(self)
            models.session.commit()
            return True
        except Exception as e:
            print(e)
            return False
